package b.h.y.bj.fragment;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import java.util.ArrayList;

import b.h.y.bj.R;
import b.h.y.bj.base.BasePager;
import b.h.y.bj.base.impl.GovAffairsPager;
import b.h.y.bj.base.impl.HomePager;
import b.h.y.bj.base.impl.NewsCenterPager;
import b.h.y.bj.base.impl.SettingPager;
import b.h.y.bj.base.impl.SmartServicePager;

/**
 * Created by YHB on 2015/12/31.
 */
public class ContentFragment extends BaseFragment {

    private RadioGroup mRadioGroup; //底部button的按钮组

    private ViewPager mViewPager; //ViewPager


    private ArrayList<BasePager> mBasePagers = new ArrayList<BasePager>();
    private ContentAdapter contentAdapter;

    @Override
    public View initViews() {
        View view = View.inflate(mActivity, R.layout.fragment_content, null);

        mRadioGroup = (RadioGroup) view.findViewById(R.id.rg_group);
        mViewPager = (ViewPager) view.findViewById(R.id.vp_content);

        return view;
    }


    @Override
    public void initData() {
        //默认选中首页
        mRadioGroup.check(R.id.rb_home);

        //初始化5个子页面
        mBasePagers.add(new HomePager(mActivity));
        mBasePagers.add(new NewsCenterPager(mActivity));
        mBasePagers.add(new SmartServicePager(mActivity));
        mBasePagers.add(new GovAffairsPager(mActivity));
        mBasePagers.add(new SettingPager(mActivity));

        //给viewPager设置Adapter
        contentAdapter = new ContentAdapter();
        mViewPager.setAdapter(contentAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                //获取当前选中的页面,初始化该页面的数据
                mBasePagers.get(position).initData();
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
        //手动初始化首页数据
        mBasePagers.get(0).initData();

        //监听radioGroup选择事件
        mRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int checkId) {
                switch (checkId) {
                    case R.id.rb_home:
                        mViewPager.setCurrentItem(0, false); //false 去掉滑动动画
                        break;
                    case R.id.rb_new:
                        mViewPager.setCurrentItem(1, false); //false 去掉滑动动画
                        break;
                    case R.id.rb_smart:
                        mViewPager.setCurrentItem(2, false); //false 去掉滑动动画
                        break;
                    case R.id.rb_govaffairs:
                        mViewPager.setCurrentItem(3, false); //false 去掉滑动动画
                        break;
                    case R.id.rb_setting:
                        mViewPager.setCurrentItem(4, false); //false 去掉滑动动画
                        break;
                }
            }
        });
    }


    class ContentAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return mBasePagers.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            BasePager pager = mBasePagers.get(position);
            //初始化数据
            //pager.initData(); 此处不要初始化数据,否则会预加载

            View view = pager.mRootView;
            container.addView(view);
            return pager.mRootView;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    /**
     * 获取新闻中心
     * @return
     */
    public NewsCenterPager getNewsCenterPager(){
        return (NewsCenterPager) mBasePagers.get(1);
    }
}
